home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 8.7 KB | 313 lines |
- package symantec.itools.multimedia;
-
- import java.awt.Dimension;
- import java.awt.Event;
- import java.awt.Graphics;
- import java.awt.Panel;
- import java.net.MalformedURLException;
- import java.util.Vector;
- import java.awt.Image;
- import java.net.URL;
-
-
- /**
- * This is a basic graphical image "slide show" component. Displays a series of images
- * with descriptive text.
- * @version 1.0, Nov 26, 1996
- * @author Symantec
- */
-
- public class SlideShow
- extends Panel
- {
- private int imageIndex;
- private ImageViewer imageViewer;
-
- private Vector urlList = new Vector();
- private Vector images = new Vector();
- private Vector descriptions = new Vector();
-
- /**
- * Constructs a new SlideShow.
- */
- public SlideShow()
- {
- imageViewer = new ImageViewer();
- imageIndex = 0;
- }
-
- /**
- * Display the first image.
- */
- public void display()
- {
- imageIndex = 0;
-
- try
- {
- images.setElementAt(getToolkit().getImage((URL)urlList.elementAt(0)), imageIndex);
- imageViewer.setImage((Image)images.elementAt(0));
- add(imageViewer);
- repaint();
- validate();
- }
- catch (Exception e)
- {
- System.err.println("Error in SlideShow: Couldn't get image " + ((URL)urlList.elementAt(imageIndex)).toString());
- }
- }
-
- /**
- * Return the number of images in the slide show set.
- */
- public int getNumberOfImages()
- {
- return urlList.size();
- }
-
- /**
- * Return the current image index being displayed.
- */
- public int getCurrentImageIndex()
- {
- return imageIndex;
- }
-
- /**
- * Add an image URL and associated description to the slide
- * show image set.
- * @param url URL of image file
- * @param description description of image
- * @return int - index of added image in slide show set
- */
- public int addImageAndDescription(URL url, String description)
- {
- urlList.addElement(url);
- descriptions.addElement(description);
- images.addElement(null);
-
- int index = urlList.size() - 1;
- if (index == 0)
- display();
-
- return index;
- }
-
- /**
- * Return the URL of the image at the given index.
- * @param index index of image to retrieve URL of
- * @return URL - URL of image at given index
- */
- public URL getURL(int index)
- {
- return (URL)urlList.elementAt(index);
- }
-
- /**
- * Return the description of the image at the given index.
- * @param index index of image to retrieve description of
- * @return String - description of image at given index
- * @see #setDescription
- */
- public String getDescription(int index)
- {
- return (String)descriptions.elementAt(index);
- }
-
- /**
- * Set the description of the image at the given index.
- * @param index index of description to set
- * @param str description string
- * @see #getDescription
- */
- public void setDescription(int index, String str)
- {
- descriptions.setElementAt(str, index);
- }
-
- /**
- * Query if displaying last image in slide show set.
- * @return boolean - true if displaying last image; false
- * otherwise
- * @see #isAtFirstImage
- */
- public boolean isAtLastImage()
- {
- return imageIndex == (urlList.size() - 1);
- }
-
- /**
- * Query if displaying first image in slide show set.
- * @return boolean - true if displaying first image; false
- * otherwise
- * @see #isAtLastImage
- */
- public boolean isAtFirstImage()
- {
- return imageIndex == 0;
- }
-
- /**
- * Display the next image in the slide show set.
- * @see #previousImage
- */
- public int nextImage()
- {
- //try
- {
- if (! isAtLastImage())
- {
- int rv;
- ++imageIndex;
- rv = setImage(imageIndex);
- getParent().deliverEvent(new Event(this, Event.ACTION_EVENT, new Integer(imageIndex)));
- return rv;
- }
- }
- /*
- catch (MalformedURLException e)
- {
- System.out.println("Error getting image");
- }
- */
- return imageIndex;
- }
-
- /**
- * Display the previous image in the slide show set.
- * @see #nextImage
- */
- public int previousImage()
- {
- //try
- {
- if (! isAtFirstImage())
- {
- int rv;
- --imageIndex;
- rv = setImage(imageIndex);
- getParent().deliverEvent(new Event(this, Event.ACTION_EVENT, new Integer(imageIndex)));
- return rv;
- }
- }
- /*
- catch (MalformedURLException e)
- {
- System.out.println("Error getting image");
- }
- */
- return imageIndex;
- }
-
- /**
- * Display the image at the given index.
- * @param index index of the image to display
- * @param str description string
- * @return int - image index being displayed
- */
- public int setImage(int index)
- //throws MalformedURLException
- {
- if (index >= 0 && index < urlList.size())
- {
- // Load the new image if we do not have it.
- try
- {
- imageIndex = index;
- if (images.elementAt(imageIndex) == null)
- images.setElementAt(getToolkit().getImage((URL)urlList.elementAt(imageIndex)), imageIndex);
- }
- catch (Exception e)
- {
- //throw new MalformedURLException("Error loading image");
- System.err.println("Error in SlideShow: Couldn't get image " + ((URL)urlList.elementAt(imageIndex)).toString());
- }
- imageViewer.setImage((Image)images.elementAt(imageIndex));
- }
-
- repaint();
-
- return imageIndex;
- }
-
- /**
- * Returns the recommended dimensions to properly display this component.
- * This is a standard Java AWT method which gets called to determine
- * the recommended size of this component.
- *
- * @return If no image has been loaded, a dimension of 10 by 10 is returned.
- * If an image has been loaded, the height and width of the image
- * is returned.
- * @see java.awt.Container#minimumSize
- */
- public Dimension preferredSize()
- {
- Dimension d;
- d = imageViewer.preferredSize();
-
- return (new Dimension(d.width, d.height));
- }
-
- /**
- * Redraws this component.
- * This is a standard Java AWT method which gets called to redraw this
- * component. It results in a call to update() as soon as possible.
- *
- * @see #update
- * @see java.awt.Component#paint
- */
- public void repaint()
- {
- super.repaint();
- imageViewer.repaint();
- }
-
- /**
- * Handles redrawing of this component on the screen.
- * This is a standard Java AWT method which gets called by the Java
- * AWT (repaint()) to handle repainting this component on the screen.
- * The graphics context clipping region is set to the bounding rectangle
- * of this component and its <0,0> coordinate is this component's
- * top-left corner.
- * Typically this method paints the background color to clear the
- * component's drawing space, sets graphics context to be the foreground
- * color, and then calls paint() to draw the component.
- *
- * It is overridden here to reduce flicker by eliminating the uneeded
- * clearing of the background.
- *
- * @param g the graphics context
- * @see #repaint
- * @see java.awt.Component#paint
- */
- public void update(Graphics g)
- {
- paint(g);
- }
-
- /**
- * Reset slide show and add first image.
- *
- * @param url url of first slide image, if null slide show is reinitialized
- */
- public void setFirstImage(URL url)
- {
- if (url == null) {
- imageViewer.setImage(null);
- imageIndex = 0;
- urlList = new Vector();
- images = new Vector();
- descriptions = new Vector();
- } else {
- if (urlList.size() != 0) {
- urlList.setElementAt(url, 0);
- descriptions.setElementAt("", 0);
- images.setElementAt(null, 0);
- setImage(0);
- } else {
- addImageAndDescription(url, "");
- }
- }
- }
- }
-